package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.annotation.AutoFill;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.enumeration.OperationType;
import com.sky.exception.BaseException;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    DishMapper dishMapper;
    @Autowired
    DishFlavorMapper dishFlavorMapper;

    @Autowired
    SetmealDishMapper setmealDishMapper;


    /*
     添加菜品(包括口味)
     * */
    @Override
    @Transactional
    public void saveWithFlavors(DishDTO dishDTO) {

        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        //添加一条菜品
        dishMapper.insert(dish);
        //useGeneratedKeys="true" keyProperty="id"
        Long dishId = dish.getId();
        //添加n个口味
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors!=null&&flavors.size()!=0)
        {
            flavors.forEach(f->{
                f.setDishId(dishId);
            });
            dishFlavorMapper.insertBatch(flavors);
        }
    }
    /*
      分页展示
      * */
    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO> page= dishMapper.pageQuery(dishPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /*
    批量删除菜品
    * */
    @Transactional
    @Override
    public void deleteBatch(List ids) {
         //启售的菜品不能删除
        List<Dish> dishes= dishMapper.getByIds(ids);
        for (Dish dish:dishes)
        {
            if (dish.getStatus()==1)
            {
                throw  new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        dishMapper.deleteBatch(ids);

      //有套餐关联的不能删除
      List<SetmealDish> setmealDishes= setmealDishMapper.getByDishIds(ids);
      if (setmealDishes!=null && setmealDishes.size()!=0)
      {
          throw  new  DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
      }

      //删除菜品的同时还要删除相关口味
        dishFlavorMapper.deleteBatchByDishId(ids);
    }


    /*
    获取菜品基本信息和对应口味(回显)
   * */
    @Override
    public DishVO getByIdWithFlavor(Long id) {
        //获取菜品
        Dish dish=dishMapper.getById(id);
        //获取菜品对应的口味
        List<DishFlavor> dishFlavors= dishFlavorMapper.getByDishId(id);
        //封装DishVO
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavors);
        return dishVO;
    }

    /*
       修改菜品基本信息和口味
     **/
    @Transactional
    @Override
    public void updateWithFlavor(DishDTO dishDTO) {
        //修改Dish表
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);
         //删除原来的flavor
        dishFlavorMapper.deleteByDishId(dishDTO.getId());
         //添加新的flavor
         List<DishFlavor> flavors = dishDTO.getFlavors();
         if (flavors!=null&&flavors.size()!=0)
         {
             flavors.forEach(f->{
                 f.setDishId(dishDTO.getId());
             });
             dishFlavorMapper.insertBatch(flavors);
         }
    }

    @AutoFill(OperationType.UPDATE)
    @Override
    public void status(Integer status, Long id) {
         Dish dish = Dish.builder().status(status).id(id).build();
         dishMapper.update(dish);
    }

    @Override
    public List<Dish> getByCategoryId(Long categoryId) {
       List<Dish> dishes= dishMapper.getByCategoryId(categoryId);
        return dishes;
    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);
        List<DishVO> dishVOList = new ArrayList<>();
        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);
            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());
            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
}
